package doan.chuyennganh.base;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import doan.chuyennganh.activity.R;
import doan.chuyennganh.net.Api;
import doan.chuyennganh.net.IApi;
import doan.chuyennganh.util.SharedPreferenceHelper;

public class DoAnBaseAcitvity extends FragmentActivity {

	public IApi daApi;
	public SharedPreferenceHelper pre;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.getSupportFragmentManager().beginTransaction()
				.add(android.R.id.content, new Fragment()).commit();
		daApi = Api.createInstance(this.getApplicationContext(),
				getString(R.string.base_url));
		pre = new SharedPreferenceHelper(this);
	}

	public RequestQueue getRequestQueue() {
		return ((IRequestQueue) getApplication()).getRequestQueue();
	}

	protected boolean isInternetAccess() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	public static float convertDpToPixel(float dp, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi / 160f);
		return px;
	}

	public static float convertPixelsToDp(float px, Context context) {
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float dp = px / (metrics.densityDpi / 160f);
		return dp;
	}

	protected void enableDialogWarning(Context context, String title,
			String message, final boolean isFinish) {
		AlertDialog.Builder builder;
		builder = new AlertDialog.Builder(context);
		final AlertDialog dialog = builder.create();
		LayoutInflater inflater = LayoutInflater.from(context);
		View layout = inflater.inflate(R.layout.dialog_warning_ok, null);
		dialog.setView(layout, 0, 0, 0, 0);
		TextView tvTitle = (TextView) layout
				.findViewById(R.id.dialog_warning_lltheader_tvTitle);
		tvTitle.setText(title);
		TextView tvQuestion = (TextView) layout
				.findViewById(R.id.dialog_warning_tvQuestion);
		tvQuestion.setText(message);
		Button btnOK = (Button) layout
				.findViewById(R.id.dialog_internet_access_lltfooter_btOK);
		
		btnOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				if(isFinish){
					finish();
				}
			}
		});
		dialog.setCancelable(false);
		dialog.show();
	}
	@Override
	protected void onPause() {
		System.gc();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		Runtime.getRuntime().gc();
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		System.exit(0);
		super.onBackPressed();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				|| keyCode == KeyEvent.KEYCODE_HOME) {
			System.exit(0);
		}
		return super.onKeyDown(keyCode, event);
	}

}
